var environment=_gl.environment||"production";
var ajaxUrl={
    "local":{
        "settimeoutTime":1400,
        "dataList_all":_gl._host_res+"/res/js/data/courseList.json",
        "updateCourse":_gl._host_res+"/res/js/data/updateCourse.json",
        "delCourse":_gl._host_res+"/res/js/data/delCourse.json?id=",

        "dataList_prev":_gl._host_res+"/res/js/data/courseList.json",
        "dataList_current":_gl._host_res+"/res/js/data/courseList_1.json",
        "dataList_next":_gl._host_res+"/res/js/data/courseList_1.json"
    },
    "production":{
        "settimeoutTime":0,
        "dataList_all":_gl._host_api+"/courses",
        "updateCourse":_gl._host_api+"/courses/",
        "delCourse":_gl._host_api+"/courses/",

        "dataList_prev":_gl._host_api+"/courses",
        "dataList_current":_gl._host_api+"/courses",
        "dataList_next":_gl._host_api+"/courses"
    }
};
$(function(){
    _fridayTable.setTableTr({
        "url":ajaxUrl[environment].dataList_all,
        "columns":[
            {
                "fieldName":null,
                "dom":"seq"
            },
            {
                "fieldName":"title"
            },
            {
                "fieldName":"course_time",
                "formatFun": _helper.timeFormat
            },
            {
                "fieldName":"create_time",
                "formatFun": _helper.timeFormat
            },
            {
                "fieldName":"status",
                "formatFun":  _helper.statusFormat
            },
            {
                "fieldName":"status",
                "renderFun": function (val) {
                    var s = '<a class="btn btn-success btn-sm btn-operation btn-query" href="#"><span class="glyphicon glyphicon-search"></span>查看</a>';
                    if(!val){
                        s+= '<a class="btn btn-success btn-sm btn-operation btn-update-up" href="#"><span class="glyphicon glyphicon-edit"></span>上架</a><a class="btn btn-primary btn-sm btn-operation btn-del" href="#"><span class="glyphicon glyphicon-remove"></span>删除</a>';
                    }else{
                        s+= '<a class="btn btn-success btn-sm btn-operation btn-update-down" href="#"><span class="glyphicon glyphicon-edit"></span>下架</a>';
                    }
                    return s
                }
            }
        ]
    });
    _fridayTable.searchData();

    //标签页
    $(".nav-tabs>li").on("click",function(e){
        e.stopPropagation();
        var $this = $(this);
        $this.addClass("active").siblings().removeClass("active");
        _type = $this.find("a").attr("href").replace("#","");
        var typeV = "";
        if(_type=="current"){
            typeV=0;
        }else if(_type=="next"){
            typeV=2;
        }else if(_type=="prev"){
            typeV=1;
        }
        $("#searchForm").find("input[type='hidden'][name='type']").val(typeV);
        _fridayTable.searchData({"url":ajaxUrl[environment]["dataList_"+_type]});
    });

    var $removeTr = null;
    var itemId="";

    /*页面按钮 查询 新增课程 更多 上一页 下一页 上架 下架 查看 删除*/
    $(document).on( 'click','.content-info a,.content-info button', function (e) {
        e.stopPropagation();
        $removeTr = $(this).parents("tr");
        itemId = $removeTr.find("input[type='hidden']").val();
        if($(this).hasClass("btn-add")){
            //头部新增课程
            gotoDetail({"flag":"add","text":"课程详情"});

        }else if($(this).hasClass("btn-update-up")){
            //列表里面的课程上架
            updateItem({"id":itemId,"status":true});

        }else if($(this).hasClass("btn-update-down")){
            //列表里面的课程下架
            updateItem({"id":itemId,"status":false});

        }else if($(this).hasClass("btn-del")){
            //列表里面的课程删除
            delItem(itemId);

        }else if($(this).hasClass("btn-query")){
            //列表里面的课程查看
            queryItem(itemId);

        }else {

        }
    });

    //进入编辑页面
    function gotoDetail(obj){
        parent.addNewIframe({"href":_gl._host_res+"/course/detail.html?flag="+((obj&&obj.flag)||add)+"&id="+((obj&&obj.id)||""),"text":(obj&&obj.text)||"课程详情"});
    }
    //修改课程
    function updateItem(obj){
        if(!obj) obj={};
        var tips="确定要下架吗？";
        if(obj.status){
            tips="确定要上架吗？";
        }
        layer.confirm(tips, {icon: 3, title:'修改提示',offset: 't'}, function(index){
            layer.close(index);
            var loadingIndex = _helper.fridayLoadinFun();
            setTimeout(function(){
                $.ajax({
                    type: "PUT",
                    url:ajaxUrl[environment].updateCourse+obj.id+"/status",
                    dataType: "json"
                }).done(function(data) {
                    if(data && data.code==200){
                        _helper.ajaxMessage({"message":"修改成功！","callback":function(){}});
                        $removeTr.find("td").eq(4).html((obj.status?"已上架":"未上架"));
                        if($removeTr.find(".btn-update-down").length>0){
                            $removeTr.find(".btn-update-down").removeClass("btn-update-down").addClass("btn-update-up").html('<span class="glyphicon glyphicon-edit"></span>上架').parent().append('<a class="btn btn-primary btn-sm btn-operation btn-del" href="#"><span class="glyphicon glyphicon-remove"></span>删除</a>');
                        }else{
                            $removeTr.find(".btn-update-up").removeClass("btn-update-up").addClass("btn-update-down").html('<span class="glyphicon glyphicon-edit"></span>下架').parent().find(".btn-del").remove();
                        }
                        $removeTr=null;
                    }else{
                        _helper.ajaxMessage({"message":(data&&data.message)||"修改失败！"});
                    }
                }).fail(function(){
                    _helper.ajaxErrorTips({"tips":"修改失败，请稍后再试！"});
                }).always(function(){
                    layer.close(loadingIndex);
                });
            },ajaxUrl[environment].settimeoutTime);
        });
    }

    //查看课程
    function queryItem(id){
        gotoDetail({"flag":"query","id":id,"text":"课程详情"});
    }

    //删除课程
    function delItem(id){
        layer.confirm('确定要删除该课程吗?', {icon: 3, title:'删除提示',offset: 't'}, function(index){
            layer.close(index);
            var loadingIndex = _helper.fridayLoadinFun();
            setTimeout(function(){
                $.ajax({
                    type: "DELETE",
                    url:ajaxUrl[environment].delCourse + id,
                    dataType: "json"
                }).done(function(data) {
                    if(data && data.code==200){
                        _helper.ajaxMessage({"message":"删除成功！","callback":function(){}});
                        $removeTr.remove();
                        $removeTr=null;
                    }else{
                        _helper.ajaxMessage({"message":(data&&data.message)||"删除失败！"});
                    }
                }).fail(function(){
                    _helper.ajaxErrorTips({"tips":"删除失败，请稍后再试！"});
                }).always(function(){
                    layer.close(loadingIndex);
                });
            },ajaxUrl[environment].settimeoutTime);
        });
    }

});